package tc.jv.task11;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import tc.jv.task4.Vector;
import tc.jv.task8.Vectors;
import tc.jv.task9.IncompatibleVectorSizeException;

public class Main
{
	
	private static String CURRENT_PATH = "E:\\workspace\\NetCracker\\src\\tc\\"
									   + "jv\\task11\\";

	public static void main(String[] args)
	{
		
		String input  = args[0],
			   output = args[1];
		
		Vector v1, v2;
		
		FileWriter fw = null;
		FileReader fr = null;
		try
		{
			fr = new FileReader(CURRENT_PATH + input);
			fw = new FileWriter(CURRENT_PATH + output);
			
			while (fr.ready())
			{
				v1 = null;
				v2 = null;
				
				v1 = Vectors.readVector(fr);
				v1.print(1);
				v2 = Vectors.readVector(fr);
				v2.print(1);
				
				if (v2.size() == 0)
				{
					System.err.println("Error: Нечетное количество векторов!");
					throw new IllegalArgumentException();
				}
				
				Server.main(null);
				new Client(12011);
				
				System.out.println("End");
				
				try
				{
					Vectors.writeVector(Vectors.add(v1, v2), fw);
					fw.flush();
				}
				catch (IncompatibleVectorSizeException e)
				{
					e.printStackTrace();
					System.err.println(v1.toString(1));
					System.err.println(v2.toString(1));
				}
			}
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try {fw.close(); fr.close();} catch (IOException e) {e.printStackTrace();}
		}
	}

}
